unit MainForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ActnList, ComCtrls, StdCtrls,
  Task, ExtCtrls;

type
  TForm1 = class(TForm)
    TimeLeftLabel: TLabel;
    TimeLeftProgressBar: TProgressBar;
    StartToggleButton: TButton;
    TasksListView: TListView;
    AddTaskButton: TButton;
    RemoveTaskButton: TButton;
    ActionList: TActionList;
    AddTaskAction: TAction;
    RemoveTaskAction: TAction;
    PauseTaskAction: TAction;
    StartTaskAction: TAction;
    EditTaskAction: TAction;
    EditTaskButton: TButton;
    Timer: TTimer;
    ResumeTaskAction: TAction;
    procedure AddTaskActionExecute(Sender: TObject);
    procedure EditTaskActionExecute(Sender: TObject);
    procedure RemoveTaskActionUpdate(Sender: TObject);
    procedure StartTaskActionUpdate(Sender: TObject);
    procedure EditTaskActionUpdate(Sender: TObject);
    procedure RemoveTaskActionExecute(Sender: TObject);
    procedure StartTaskActionExecute(Sender: TObject);
    procedure TimerTimer(Sender: TObject);
    procedure ResumeTaskActionExecute(Sender: TObject);
    procedure PauseTaskActionExecute(Sender: TObject);
  private
    FRemainingSeconds: Integer;
    function AddNewItem: TListItem;
    procedure UpdateListItem(const ListItem: TListItem; const Task: TTask);
    procedure UpdateTimeLabel;
    procedure InitializeProgressBar;
    procedure DecreaseRemainingTime;
    procedure StopCurrentTask;
    procedure FocusOnNextTask;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses
  TaskDetailsForm,
  TimeFormat;

{$R *.dfm}

function TForm1.AddNewItem: TListItem;
begin
  Result := TasksListView.Items.Add;
  Result.SubItems.Add('');
end;

procedure TForm1.AddTaskActionExecute(Sender: TObject);
var
  NewTask: TTask;
  ListItem: TListItem;
begin
  NewTask := TTask.CreateWithValues('New Task', 10);
  if TTaskDetailsForm.EditTask(NewTask) then
  begin
    ListItem := AddNewItem;
    UpdateListItem(ListItem, NewTask);
  end;
end;

procedure TForm1.DecreaseRemainingTime;
begin
  Dec(FRemainingSeconds);
  UpdateTimeLabel;
  TimeLeftProgressBar.Position := TimeLeftProgressBar.Max - FRemainingSeconds;
end;

procedure TForm1.EditTaskActionExecute(Sender: TObject);
var
  Task: TTask;
  ListItem: TListItem;
begin
  ListItem := TasksListView.Selected;
  Task := TTask(ListItem.Data);
  if TTaskDetailsForm.EditTask(Task) then
    UpdateListItem(ListItem, Task);
end;

procedure TForm1.EditTaskActionUpdate(Sender: TObject);
begin
  EditTaskAction.Enabled := Assigned(TasksListView.Selected);
end;

procedure TForm1.FocusOnNextTask;
var
  OldListItem, NewListItem: TListItem;
  Task: TTask;
begin
  OldListItem := TasksListView.Items[0];
  Task := OldListItem.Data;
  OldListItem.Free;
  NewListItem := AddNewItem;
  UpdateListItem(NewListItem, Task);
end;

procedure TForm1.InitializeProgressBar;
begin
  TimeLeftProgressBar.Min := 0;
  TimeLeftProgressBar.Max := FRemainingSeconds;
  TimeLeftProgressBar.Position := 0;
end;

procedure TForm1.PauseTaskActionExecute(Sender: TObject);
begin
  StartToggleButton.Action := ResumeTaskAction;
  Timer.Enabled := false;
end;

procedure TForm1.RemoveTaskActionExecute(Sender: TObject);
var
  Task: TTask;
  ListItem: TListItem;
begin
  ListItem := TasksListView.Selected;
  Task := TTask(ListItem.Data);
  Task.Free;
  ListItem.Free;
end;

procedure TForm1.RemoveTaskActionUpdate(Sender: TObject);
begin
  RemoveTaskAction.Enabled := Assigned(TasksListView.Selected);
end;

procedure TForm1.ResumeTaskActionExecute(Sender: TObject);
begin
  StartToggleButton.Action := PauseTaskAction;
  Timer.Enabled := true;
end;

procedure TForm1.StartTaskActionExecute(Sender: TObject);
var
  FirstTask: TTask;
begin
  FirstTask := TasksListView.Items[0].Data;
  FRemainingSeconds := FirstTask.Duration * 60;
  UpdateTimeLabel;
  InitializeProgressBar;
  Timer.Enabled := true;
  StartToggleButton.Action := PauseTaskAction;
end;

procedure TForm1.StartTaskActionUpdate(Sender: TObject);
begin
  StartTaskAction.Enabled := TasksListView.Items.Count > 0;
end;

procedure TForm1.StopCurrentTask;
begin
  StartToggleButton.Action := StartTaskAction;
  Timer.Enabled := false;
  if TasksListView.Items.Count > 1 then
    FocusOnNextTask;
end;

procedure TForm1.TimerTimer(Sender: TObject);
begin
  if FRemainingSeconds > 0 then
    DecreaseRemainingTime
  else
    StopCurrentTask;
end;

procedure TForm1.UpdateListItem(const ListItem: TListItem; const Task: TTask);
begin
  ListItem.Caption := Task.Name;
  ListItem.SubItems[0] := TTimeFormat.FormatMinutes(Task.Duration);
  ListItem.Data := Task;
end;

procedure TForm1.UpdateTimeLabel;
begin
  TimeLeftLabel.Caption := TTimeFormat.FormatSeconds(FRemainingSeconds);
end;

end.
